{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split, GridSearchCV\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.neighbors import KNeighborsClassifier"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 1.获取数据集\n",
    "# 2.数据基本处理\n",
    "# 2.1 缩小数据范围\n",
    "# 2.2 选择时间特征\n",
    "# 2.3 去掉签到少的地方\n",
    "# 2.4 确定特征值和目标值\n",
    "# 2.5 分割数据集\n",
    "# 3.特征工程 -- 特征预处理（标准化）\n",
    "# 4.机器学习 -- knn+cv\n",
    "# 5.模型评估"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "data = pd.read_csv(r\"E:\\data\\machine_learning\\FBlocation\\train.csv\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "   row_id       x       y  accuracy    time    place_id\n0       0  0.7941  9.0809        54  470702  8523065625\n1       1  5.9567  4.7968        13  186555  1757726713\n2       2  8.3078  7.0407        74  322648  1137537235\n3       3  7.3665  2.5165        65  704587  6567393236\n4       4  4.0961  1.1307        31  472130  7440663949",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>row_id</th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>time</th>\n      <th>place_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>0.7941</td>\n      <td>9.0809</td>\n      <td>54</td>\n      <td>470702</td>\n      <td>8523065625</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>5.9567</td>\n      <td>4.7968</td>\n      <td>13</td>\n      <td>186555</td>\n      <td>1757726713</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2</td>\n      <td>8.3078</td>\n      <td>7.0407</td>\n      <td>74</td>\n      <td>322648</td>\n      <td>1137537235</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>3</td>\n      <td>7.3665</td>\n      <td>2.5165</td>\n      <td>65</td>\n      <td>704587</td>\n      <td>6567393236</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>4</td>\n      <td>4.0961</td>\n      <td>1.1307</td>\n      <td>31</td>\n      <td>472130</td>\n      <td>7440663949</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "             row_id             x             y      accuracy          time  \\\ncount  2.911802e+07  2.911802e+07  2.911802e+07  2.911802e+07  2.911802e+07   \nmean   1.455901e+07  4.999770e+00  5.001814e+00  8.284912e+01  4.170104e+05   \nstd    8.405649e+06  2.857601e+00  2.887505e+00  1.147518e+02  2.311761e+05   \nmin    0.000000e+00  0.000000e+00  0.000000e+00  1.000000e+00  1.000000e+00   \n25%    7.279505e+06  2.534700e+00  2.496700e+00  2.700000e+01  2.030570e+05   \n50%    1.455901e+07  5.009100e+00  4.988300e+00  6.200000e+01  4.339220e+05   \n75%    2.183852e+07  7.461400e+00  7.510300e+00  7.500000e+01  6.204910e+05   \nmax    2.911802e+07  1.000000e+01  1.000000e+01  1.033000e+03  7.862390e+05   \n\n           place_id  \ncount  2.911802e+07  \nmean   5.493787e+09  \nstd    2.611088e+09  \nmin    1.000016e+09  \n25%    3.222911e+09  \n50%    5.518573e+09  \n75%    7.764307e+09  \nmax    9.999932e+09  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>row_id</th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>time</th>\n      <th>place_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>count</th>\n      <td>2.911802e+07</td>\n      <td>2.911802e+07</td>\n      <td>2.911802e+07</td>\n      <td>2.911802e+07</td>\n      <td>2.911802e+07</td>\n      <td>2.911802e+07</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>1.455901e+07</td>\n      <td>4.999770e+00</td>\n      <td>5.001814e+00</td>\n      <td>8.284912e+01</td>\n      <td>4.170104e+05</td>\n      <td>5.493787e+09</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>8.405649e+06</td>\n      <td>2.857601e+00</td>\n      <td>2.887505e+00</td>\n      <td>1.147518e+02</td>\n      <td>2.311761e+05</td>\n      <td>2.611088e+09</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>0.000000e+00</td>\n      <td>0.000000e+00</td>\n      <td>0.000000e+00</td>\n      <td>1.000000e+00</td>\n      <td>1.000000e+00</td>\n      <td>1.000016e+09</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>7.279505e+06</td>\n      <td>2.534700e+00</td>\n      <td>2.496700e+00</td>\n      <td>2.700000e+01</td>\n      <td>2.030570e+05</td>\n      <td>3.222911e+09</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>1.455901e+07</td>\n      <td>5.009100e+00</td>\n      <td>4.988300e+00</td>\n      <td>6.200000e+01</td>\n      <td>4.339220e+05</td>\n      <td>5.518573e+09</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>2.183852e+07</td>\n      <td>7.461400e+00</td>\n      <td>7.510300e+00</td>\n      <td>7.500000e+01</td>\n      <td>6.204910e+05</td>\n      <td>7.764307e+09</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>2.911802e+07</td>\n      <td>1.000000e+01</td>\n      <td>1.000000e+01</td>\n      <td>1.033000e+03</td>\n      <td>7.862390e+05</td>\n      <td>9.999932e+09</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.describe()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "(29118021, 6)"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "# 2.1 缩小数据范围\n",
    "partial_data = data.query(\"x>2.0&x<2.5&y>2.0&y<2.5\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "      row_id       x       y  accuracy    time    place_id\n163      163  2.1663  2.3755        84  669737  3869813743\n310      310  2.3695  2.2034         3  234719  2636621520\n658      658  2.3236  2.1768        66  502343  7877745055\n1368    1368  2.2613  2.3392        73  319822  9775192577\n1627    1627  2.3331  2.0011        66  595084  6731326909",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>row_id</th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>time</th>\n      <th>place_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>163</th>\n      <td>163</td>\n      <td>2.1663</td>\n      <td>2.3755</td>\n      <td>84</td>\n      <td>669737</td>\n      <td>3869813743</td>\n    </tr>\n    <tr>\n      <th>310</th>\n      <td>310</td>\n      <td>2.3695</td>\n      <td>2.2034</td>\n      <td>3</td>\n      <td>234719</td>\n      <td>2636621520</td>\n    </tr>\n    <tr>\n      <th>658</th>\n      <td>658</td>\n      <td>2.3236</td>\n      <td>2.1768</td>\n      <td>66</td>\n      <td>502343</td>\n      <td>7877745055</td>\n    </tr>\n    <tr>\n      <th>1368</th>\n      <td>1368</td>\n      <td>2.2613</td>\n      <td>2.3392</td>\n      <td>73</td>\n      <td>319822</td>\n      <td>9775192577</td>\n    </tr>\n    <tr>\n      <th>1627</th>\n      <td>1627</td>\n      <td>2.3331</td>\n      <td>2.0011</td>\n      <td>66</td>\n      <td>595084</td>\n      <td>6731326909</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partial_data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "(71664, 6)"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partial_data.shape"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "163     669737\n310     234719\n658     502343\n1368    319822\n1627    595084\nName: time, dtype: int64"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.2 选择时间特征\n",
    "partial_data[\"time\"].head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "163    1970-01-08 18:02:17\n310    1970-01-03 17:11:59\n658    1970-01-06 19:32:23\n1368   1970-01-04 16:50:22\n1627   1970-01-07 21:18:04\nName: time, dtype: datetime64[ns]"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time = pd.to_datetime(partial_data[\"time\"], unit=\"s\")\n",
    "time.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['1970-01-08 18:02:17', '1970-01-03 17:11:59',\n               '1970-01-06 19:32:23', '1970-01-04 16:50:22',\n               '1970-01-07 21:18:04', '1970-01-02 03:14:59',\n               '1970-01-07 03:45:16', '1970-01-05 03:28:43',\n               '1970-01-01 18:59:03', '1970-01-09 07:50:12',\n               ...\n               '1970-01-09 20:03:34', '1970-01-08 09:26:50',\n               '1970-01-07 04:45:59', '1970-01-07 22:36:18',\n               '1970-01-06 23:29:43', '1970-01-03 12:31:26',\n               '1970-01-04 15:19:20', '1970-01-01 20:49:14',\n               '1970-01-03 09:17:37', '1970-01-02 20:34:43'],\n              dtype='datetime64[ns]', name='time', length=71664, freq=None)"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time = pd.DatetimeIndex(time)\n",
    "time"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\wlw\\AppData\\Local\\Temp\\ipykernel_24548\\654291739.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  partial_data[\"hour\"]=time.hour\n",
      "C:\\Users\\wlw\\AppData\\Local\\Temp\\ipykernel_24548\\654291739.py:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  partial_data[\"day\"]=time.day\n",
      "C:\\Users\\wlw\\AppData\\Local\\Temp\\ipykernel_24548\\654291739.py:3: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  partial_data[\"weekday\"]=time.weekday\n"
     ]
    }
   ],
   "source": [
    "partial_data[\"hour\"]=time.hour\n",
    "partial_data[\"day\"]=time.day\n",
    "partial_data[\"weekday\"]=time.weekday"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "      row_id       x       y  accuracy    time    place_id  hour  day  weekday\n163      163  2.1663  2.3755        84  669737  3869813743    18    8        3\n310      310  2.3695  2.2034         3  234719  2636621520    17    3        5\n658      658  2.3236  2.1768        66  502343  7877745055    19    6        1\n1368    1368  2.2613  2.3392        73  319822  9775192577    16    4        6\n1627    1627  2.3331  2.0011        66  595084  6731326909    21    7        2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>row_id</th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>time</th>\n      <th>place_id</th>\n      <th>hour</th>\n      <th>day</th>\n      <th>weekday</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>163</th>\n      <td>163</td>\n      <td>2.1663</td>\n      <td>2.3755</td>\n      <td>84</td>\n      <td>669737</td>\n      <td>3869813743</td>\n      <td>18</td>\n      <td>8</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>310</th>\n      <td>310</td>\n      <td>2.3695</td>\n      <td>2.2034</td>\n      <td>3</td>\n      <td>234719</td>\n      <td>2636621520</td>\n      <td>17</td>\n      <td>3</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>658</th>\n      <td>658</td>\n      <td>2.3236</td>\n      <td>2.1768</td>\n      <td>66</td>\n      <td>502343</td>\n      <td>7877745055</td>\n      <td>19</td>\n      <td>6</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1368</th>\n      <td>1368</td>\n      <td>2.2613</td>\n      <td>2.3392</td>\n      <td>73</td>\n      <td>319822</td>\n      <td>9775192577</td>\n      <td>16</td>\n      <td>4</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>1627</th>\n      <td>1627</td>\n      <td>2.3331</td>\n      <td>2.0011</td>\n      <td>66</td>\n      <td>595084</td>\n      <td>6731326909</td>\n      <td>21</td>\n      <td>7</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partial_data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [],
   "source": [
    "# 2.3 去掉签到少的地方\n",
    "place_count = partial_data.groupby(\"place_id\").count()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "            row_id    x    y  accuracy  time  hour  day  weekday\nplace_id                                                        \n1006234733       1    1    1         1     1     1    1        1\n1008823061       4    4    4         4     4     4    4        4\n1012580558       3    3    3         3     3     3    3        3\n1025585791      21   21   21        21    21    21   21       21\n1026507711     220  220  220       220   220   220  220      220",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>row_id</th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>time</th>\n      <th>hour</th>\n      <th>day</th>\n      <th>weekday</th>\n    </tr>\n    <tr>\n      <th>place_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1006234733</th>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1008823061</th>\n      <td>4</td>\n      <td>4</td>\n      <td>4</td>\n      <td>4</td>\n      <td>4</td>\n      <td>4</td>\n      <td>4</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>1012580558</th>\n      <td>3</td>\n      <td>3</td>\n      <td>3</td>\n      <td>3</td>\n      <td>3</td>\n      <td>3</td>\n      <td>3</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>1025585791</th>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n    </tr>\n    <tr>\n      <th>1026507711</th>\n      <td>220</td>\n      <td>220</td>\n      <td>220</td>\n      <td>220</td>\n      <td>220</td>\n      <td>220</td>\n      <td>220</td>\n      <td>220</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "place_count.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [],
   "source": [
    "place_count = place_count[place_count[\"row_id\"]>3]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "            row_id    x    y  accuracy  time  hour  day  weekday\nplace_id                                                        \n1008823061       4    4    4         4     4     4    4        4\n1025585791      21   21   21        21    21    21   21       21\n1026507711     220  220  220       220   220   220  220      220\n1032417180      10   10   10        10    10    10   10       10\n1040557418     123  123  123       123   123   123  123      123",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>row_id</th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>time</th>\n      <th>hour</th>\n      <th>day</th>\n      <th>weekday</th>\n    </tr>\n    <tr>\n      <th>place_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1008823061</th>\n      <td>4</td>\n      <td>4</td>\n      <td>4</td>\n      <td>4</td>\n      <td>4</td>\n      <td>4</td>\n      <td>4</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>1025585791</th>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n      <td>21</td>\n    </tr>\n    <tr>\n      <th>1026507711</th>\n      <td>220</td>\n      <td>220</td>\n      <td>220</td>\n      <td>220</td>\n      <td>220</td>\n      <td>220</td>\n      <td>220</td>\n      <td>220</td>\n    </tr>\n    <tr>\n      <th>1032417180</th>\n      <td>10</td>\n      <td>10</td>\n      <td>10</td>\n      <td>10</td>\n      <td>10</td>\n      <td>10</td>\n      <td>10</td>\n      <td>10</td>\n    </tr>\n    <tr>\n      <th>1040557418</th>\n      <td>123</td>\n      <td>123</td>\n      <td>123</td>\n      <td>123</td>\n      <td>123</td>\n      <td>123</td>\n      <td>123</td>\n      <td>123</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "place_count.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [],
   "source": [
    "partial_data = partial_data[partial_data[\"place_id\"].isin(place_count.index)]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "(69264, 9)"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partial_data.shape"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [],
   "source": [
    "# 2.4 确定特征值和目标值\n",
    "x=partial_data[[\"x\",\"y\",\"accuracy\",\"hour\",\"day\",\"weekday\"]]\n",
    "y=partial_data[\"place_id\"]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [],
   "source": [
    "# 2.5 分割数据集\n",
    "x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.25,random_state=2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [],
   "source": [
    "# 3.特征工程 -- 特征预处理（标准化）\n",
    "transfer = StandardScaler()\n",
    "x_train = transfer.fit_transform(x_train)\n",
    "x_test = transfer.transform(x_test)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\Anaconda3\\envs\\wlw_3_8\\lib\\site-packages\\sklearn\\model_selection\\_split.py:737: UserWarning: The least populated class in y has only 1 members, which is less than n_splits=10.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "text/plain": "GridSearchCV(cv=10, estimator=KNeighborsClassifier(), n_jobs=-1,\n             param_grid={'n_neighbors': [3, 5, 7, 9]})",
      "text/html": "<style>#sk-container-id-1 {color: black;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>GridSearchCV(cv=10, estimator=KNeighborsClassifier(), n_jobs=-1,\n             param_grid={&#x27;n_neighbors&#x27;: [3, 5, 7, 9]})</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item sk-dashed-wrapped\"><div class=\"sk-label-container\"><div class=\"sk-label sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" ><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">GridSearchCV</label><div class=\"sk-toggleable__content\"><pre>GridSearchCV(cv=10, estimator=KNeighborsClassifier(), n_jobs=-1,\n             param_grid={&#x27;n_neighbors&#x27;: [3, 5, 7, 9]})</pre></div></div></div><div class=\"sk-parallel\"><div class=\"sk-parallel-item\"><div class=\"sk-item\"><div class=\"sk-label-container\"><div class=\"sk-label sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-2\" type=\"checkbox\" ><label for=\"sk-estimator-id-2\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">estimator: KNeighborsClassifier</label><div class=\"sk-toggleable__content\"><pre>KNeighborsClassifier()</pre></div></div></div><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-3\" type=\"checkbox\" ><label for=\"sk-estimator-id-3\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">KNeighborsClassifier</label><div class=\"sk-toggleable__content\"><pre>KNeighborsClassifier()</pre></div></div></div></div></div></div></div></div></div></div>"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4.机器学习 -- knn+cv\n",
    "estimator = KNeighborsClassifier()\n",
    "param_grip = {\"n_neighbors\":[3,5,7,9]}\n",
    "estimator = GridSearchCV(estimator=estimator, param_grid=param_grip,cv=10,n_jobs=-1)\n",
    "estimator.fit(x_train,y_train)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率为:\n",
      " 0.3693116193116193\n"
     ]
    }
   ],
   "source": [
    "# 5.模型评估\n",
    "score_ret = estimator.score(x_test,y_test)\n",
    "print(\"准确率为:\\n\", score_ret)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测值是：\n",
      " [2225211839 8980163153 1247398579 ... 1891783132 8169595806 3661555534]\n"
     ]
    }
   ],
   "source": [
    "y_pred = estimator.predict(x_test)\n",
    "print(\"预测值是：\\n\", y_pred)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在交叉验证中，得到的最好结果是：\n",
      " 0.36103403905372417\n",
      "在交叉验证中，得到的最好模型是：\n",
      " KNeighborsClassifier()\n",
      "在交叉验证中，得到的模型结果是：\n",
      " {'mean_fit_time': array([0.19707296, 0.19487891, 0.16864917, 0.15388844]), 'std_fit_time': array([0.01565637, 0.02833253, 0.02230754, 0.0148267 ]), 'mean_score_time': array([0.49457605, 0.58742769, 0.60198939, 0.50824003]), 'std_score_time': array([0.01521316, 0.0566326 , 0.04020885, 0.13064337]), 'param_n_neighbors': masked_array(data=[3, 5, 7, 9],\n",
      "             mask=[False, False, False, False],\n",
      "       fill_value='?',\n",
      "            dtype=object), 'params': [{'n_neighbors': 3}, {'n_neighbors': 5}, {'n_neighbors': 7}, {'n_neighbors': 9}], 'split0_test_score': array([0.34725698, 0.36438884, 0.36612127, 0.3599615 ]), 'split1_test_score': array([0.34687199, 0.35938402, 0.35919153, 0.35688162]), 'split2_test_score': array([0.35052936, 0.35899904, 0.3574591 , 0.35784408]), 'split3_test_score': array([0.34898941, 0.36458133, 0.36246391, 0.35899904]), 'split4_test_score': array([0.35264678, 0.36381136, 0.36458133, 0.35803657]), 'split5_test_score': array([0.34513956, 0.3572666 , 0.35688162, 0.35514918]), 'split6_test_score': array([0.35437921, 0.36477382, 0.36554379, 0.36997113]), 'split7_test_score': array([0.35264678, 0.35880654, 0.3626564 , 0.36323388]), 'split8_test_score': array([0.34501348, 0.35618021, 0.35463997, 0.35309973]), 'split9_test_score': array([0.35021178, 0.36214863, 0.3567578 , 0.35309973]), 'mean_test_score': array([0.34936853, 0.36103404, 0.36062967, 0.35862765]), 'std_test_score': array([0.00310395, 0.00310417, 0.00392977, 0.00478577]), 'rank_test_score': array([4, 1, 2, 3])}\n"
     ]
    }
   ],
   "source": [
    "print(\"在交叉验证中，得到的最好结果是：\\n\", estimator.best_score_)\n",
    "print(\"在交叉验证中，得到的最好模型是：\\n\", estimator.best_estimator_)\n",
    "print(\"在交叉验证中，得到的模型结果是：\\n\", estimator.cv_results_)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}